<?php

class StatusController extends GTL_Action {

    public function init() {
        parent::init();
        $this->lng_id = '1';
       
        /* Initialize action controller here */
    }

    public function indexAction() {
        $this->_helper->redirector('form', 'status', 'default');
    }

   

    public function formAction() {

        $successmessage = "";
        $mem_description = "";
        $errorMessageArray = Array();
        $this->view->mode = "Add";
        $data = Array();
        $propertyData = Array();
        
       
        
        $statusModel = new Model_Status();
        $statusForm = new Form_Status();
        $memberModel = new Model_Member();
        $propertyModel = new Model_Property();
        $vatModel = new Model_Vat();
       //Load client select box
        
        $statusArray = $statusModel->FetchstatusType();
        $statusForm->st_type->addMultiOptions($statusArray);
        
        //Load agent select box
        $memberArray = $memberModel->fetchMemberData();
        $statusForm->agent_id->addMultiOptions($memberArray);
        
        //Load agent select box
        $memberArray = $memberModel->fetchMemberData();
        $statusForm->referral_name->addMultiOptions($memberArray);
        
        
        //load vat in select box
        $propertyvatArray = $vatModel->fetchVat();
        $statusForm->vat_rate->addMultiOptions($propertyvatArray);
        
        $prp_id = $this->_request->getParam('prp_id');
        $propertyData = $propertyModel->fetchStatusById($prp_id);
        
       
        
        
        $statusData = Array();
        //get property id
        $statusData['prp_id'] = $this->_request->getParam('prp_id');
        
       // $statusData['st_id'] = $this->_request->getParam('st_id');
       // $st_id = (!empty($statusData['st_id'])) ? $statusData['st_id'] : null;

       $this->view->prp_rentprice =  $propertyData['prp_rentprice'];
       $this->view->prp_saleprice =  $propertyData['prp_saleprice'];
       $this->view->vat_value = $propertyData['vat_percent'];
        //set default value
        $statusForm->date_sale->setValue(date('Y-m-d'));
        $statusForm->vat_rate->setValue($propertyData['prp_vat_id']);
            
            
        $vat_value = ($propertyData['prp_saleprice'] * $propertyData['vat_percent'])/100;
        $statusForm->vat->setValue($vat_value);
        $total_price = $vat_value + $propertyData['prp_saleprice'];
        $statusForm->total_price->setValue($total_price);
        //$statusForm->agent_id->setValue($propertyData['member_id']);
        //$statusForm->referral_name->setValue($propertyData['client_referral_name']);
        
        
        
        
       $data = $statusModel->fetchEntryById($statusData['prp_id']);
       

        
        if ($data) {
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            $statusData['st_id'] =$data['st_id'];
            //Fetch data to be loaded in form 
            if($data['st_type'] == 'sold'){
                $this->view->sold_status = 1;
            }else{
                $this->view->rent_status = 1;
            }
        }
        
         $this->view->form = $statusForm->populate($data);
         if ($this->getRequest()->isPost()) {
              //Check for Errors
            $postedData = $this->_request->getPost();
            
            if (!$statusForm->isValid($postedData)) {
                $errorMessage = $statusForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            }else{
                if (count($errorMessageArray) > 0) {
                }else {
                     //Write down the code to insert and add new status
                    $statusData['st_type'] = $postedData['st_type'];
                    if($postedData['st_type'] == 'rented'){
                        $statusData['st_start_date'] = $postedData['st_start_date'];
                        $statusData['st_end_date'] = $postedData['st_end_date'];
                    }
                    
                    $statusData['date_sale'] = $postedData['date_sale'];
                    $statusData['vat_rate'] = $postedData['vat_rate'];
                    $statusData['vat'] = $postedData['vat'];
                    $statusData['total_price'] = $postedData['total_price'];
                    $statusData['agent_id'] = $postedData['agent_id'];
                    $statusData['agent_commission'] = $postedData['agent_commission'];
                    $statusData['referral_name'] = $postedData['referral_name'];
                    $statusData['referral_commission'] = $postedData['referral_commission'];
                    $statusData['other_name'] = $postedData['other_name'];
                    $statusData['other_commission'] = $postedData['other_commission'];
                    $statusData['other_costs'] = $postedData['other_costs'];
                    $statusData['landtourist_profit'] = $postedData['landtourist_profit'];
                    $statusData['st_price'] = $postedData['st_price'];
                    $statusData['st_createdate'] = date('Y-m-d H:i:s');
                   
                    $statusModel->save($statusData, 'st_id');
                   
                    if ($this->view->mode == "Add") {
                        $successmessage = 'Status add successfully';
                    } else {
                        $successmessage = 'Status updated successfully';
                    }
                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('list', 'property', '');
                    
                }
            }
            
            
         }
        
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->mode = $this->view->mode;
        $this->view->form = $statusForm;
    }
  
   
   
    public function deleteAction() {


        if ($this->_getParam('st_id') && $this->_getParam('st_id') != "") {
            $status_id = $this->_getParam('st_id');

            $statusModel = new Model_Status();
            $statusModel->delete("st_id = '" . $status_id . "'");

            $successmessage = "Status deleted successfully";
            $this->addSuccessMessage($successmessage);
            $this->_helper->redirector('list', 'status', '');
            exit;
        }
    }

    public function getvatvalueAction(){
       $vat_rate = $_POST['vat_rate'];
       $st_price = $_POST['st_price'];
       
       $vatModel = new Model_Vat();
       $vat_data = $vatModel->fetchEntryById($vat_rate);
       $vat_value = ($st_price * $vat_data['vat_percent'])/100;
       //echo $vat_value;
       echo  $vat_value;
        exit;
    }
}

   
?>
